using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Phoenix.Xna.Framework.Properties;

namespace Phoenix.Xna.Framework.Collections
{
    public class ReadOnlyDictionary<K, V> : IDictionary<K, V>
    {
        private readonly IDictionary<K, V> _innerDictionary;

        public ReadOnlyDictionary(IDictionary<K, V> dictionary)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException(string.Format(Resources.ArgumentNullException, dictionary));
            }

            _innerDictionary = dictionary;
        }

        #region IDictionary<T,V> Members

        public void Add(K key, V value)
        {
            throw new NotSupportedException(Resources.ReadOnlyCollectionException);
        }

        public bool ContainsKey(K key)
        {
            return _innerDictionary.ContainsKey(key);
        }

        public ICollection<K> Keys
        {
            get { return new ReadOnlyCollection<K>(new List<K>(_innerDictionary.Keys)); }
        }

        public bool Remove(K key)
        {
            throw new NotSupportedException(Resources.ReadOnlyCollectionException);
        }

        public bool TryGetValue(K key, out V value)
        {
            return _innerDictionary.TryGetValue(key, out value);
        }

        public ICollection<V> Values
        {
            get { return new ReadOnlyCollection<V>(new List<V>(_innerDictionary.Values)); }
        }

        public V this[K key]
        {
            get { return _innerDictionary[key]; }
            set { throw new NotSupportedException(Resources.ReadOnlyCollectionException); }
        }

        #endregion

        #region ICollection<KeyValuePair<T,V>> Members

        public void Add(KeyValuePair<K, V> item)
        {
            throw new NotSupportedException(Resources.ReadOnlyCollectionException);
        }

        public void Clear()
        {
            throw new NotSupportedException(Resources.ReadOnlyCollectionException);
        }

        public bool Contains(KeyValuePair<K, V> item)
        {
            foreach (KeyValuePair<K, V> p in _innerDictionary)
            {
                if (p.Equals(item))
                {
                    return true;
                }
            }

            return false;
        }

        public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
        {
            _innerDictionary.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _innerDictionary.Count; }
        }

        public bool IsReadOnly
        {
            get { return true; }
        }

        public bool Remove(KeyValuePair<K, V> item)
        {
            throw new NotSupportedException(Resources.ReadOnlyCollectionException);
        }

        #endregion

        #region IEnumerable<KeyValuePair<T,V>> Members

        public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }

        #endregion
    }
}